#include "CEUtilities.h"

#include "CEColor.h"


Color Color::Black = Color(0.0f, 0.0f, 0.0f);
Color Color::White = Color(1.0f, 1.0f, 1.0f);
Color Color::Red	 = Color(1.0f, 0.0f, 0.0f);	
Color Color::Green = Color(0.0f, 1.0f, 0.0f);
Color Color::Blue	 = Color(0.0f, 0.0f, 1.0f);
Color Color::NoColor = Color(0.0f, 0.0f, 0.0f, 0.0f);


//----------------------------------------------------------------------
Color Color::GetColor(int red, int green, int blue, int alpha)
{
	REAL base = 1.0f / 255;

	return Color(red * base,
				 green * base,
				 blue * base,
				 alpha * base);
}


//----------------------------------------------------------------------
RGBA Color::GetAsRGBA() const
{
	uint8 val8;
	uint32 val32 = 0;

	// Red
	val8 = static_cast<uint8>(r * 255);
	val32 = val8 << 24;

	// Green
	val8 = static_cast<uint8>(g * 255);
	val32 += val8 << 16;

	// Blue
	val8 = static_cast<uint8>(b * 255);
	val32 += val8 << 8;

	// Alpha
	val8 = static_cast<uint8>(a * 255);
	val32 += val8;

	return val32;
}


//----------------------------------------------------------------------
ARGB Color::GetAsARGB() const
{
	uint8 val8;
	uint32 val32 = 0;

	// Alpha
	val8 = static_cast<uint8>(a * 255);
	val32 = val8 << 24;
	
	// Red
	val8 = static_cast<uint8>(r * 255);
	val32 += val8 << 16;

	// Green
	val8 = static_cast<uint8>(g * 255);
	val32 += val8 << 8;

	// Blue
	val8 = static_cast<uint8>(b * 255);
	val32 += val8;

	return val32;
}


//----------------------------------------------------------------------
void Color::SetAsRGBA(RGBA colorVal)
{
	uint32 val32 = colorVal;
	
	// Red
	r = static_cast<uint8>(val32 >> 24) / 255.0f;

	// Green
	g = static_cast<uint8>(val32 >> 16) / 255.0f;

	// Blue
	b = static_cast<uint8>(val32 >> 8)  / 255.0f;

	// Alpha
	a = static_cast<uint8>(val32) / 255.0f;
}


//----------------------------------------------------------------------
void Color::SetAsARGB(ARGB colorVal)
{
	uint32 val32 = colorVal;
	
	// Red
	a = static_cast<uint8>(val32 >> 24) / 255.0f;

	// Green
	r = static_cast<uint8>(val32 >> 16) / 255.0f;

	// Blue
	g = static_cast<uint8>(val32 >> 8)  / 255.0f;

	// Alpha
	b = static_cast<uint8>(val32) / 255.0f;
}


//----------------------------------------------------------------------
D3DCOLORVALUE Color::GetAsD3DCOLOR()
{
	D3DCOLORVALUE color;
	color.r = r;
	color.g = g;
	color.b = b;
	color.a = a;

	return color;
}


//----------------------------------------------------------------------
void Color::SetAsD3DCOLORVALUE(D3DCOLORVALUE colorVal)
{
	r = colorVal.r;
	g = colorVal.g;
	b = colorVal.b;
	a = colorVal.a;
}
